/**
 * 富文本编辑器
 */
var scripts = document.getElementsByTagName("script");
var currentScriptPath = scripts[scripts.length-1].src;

app.directive('zRichTextInput',['$compile',function($compile) {
	return {
		restrict:'A',
		templateUrl: currentScriptPath.substring(0, currentScriptPath.lastIndexOf('/') + 1) +"richTextInputTemplate.html",
		replace: true,
		scope: {
			contentHtml:'=',
			onChangeHtml :'&'
		},
		link: function(scope,element,attrs,controller){
			debugger;
			scope.uploadFiles = [];
			
			scope.$watch('contentHtml',function(newValue) {
				element.parent().find('.summernote').code(newValue);
			});
			element.parent().find('.summernote').summernote({
				height: element.parent().height(),
				tabsize: 2,
				uploadFiles:scope.uploadFiles,
//				onImageUpload:function(files, editor, $editable) {
//					debugger;
//				},
				onChange : function(contents,editable$) {
					debugger;
					scope.contentHtml = contents;
					scope.onChangeHtml({content:contents});
				},
			});
			var zUpload = element.parent().find(".z-Upload");
			var zUploadBtn = element.parent().find(".note-image-btn");
			
			scope.onQueueComplete = function(files) {
				debugger;
				for(var i = 0;i<files.length;i++) {
					//alert(files[i].attachment.url);
					scope.uploadFiles.push(files[i].attachment.url);
				}
				zUploadBtn.toggleClass('disabled', false);
				zUploadBtn.attr('disabled', false);
			};
			zUpload.attr("on-Queue-Complete","onQueueComplete(fileUrl)");
			$compile(zUpload)(scope);
			debugger;
		}
	};
}]);